package com.sanjiahantang.youjiarole.module.me.order.views.impl;

import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.module.me.order.beans.LogisticsBean;
import com.sanjiahantang.youjiarole.module.me.order.presenters.OrderPresenter;
import com.sanjiahantang.youjiarole.module.me.order.views.OrderView;

/**
 * Created by zy on 2018/1/3.
 */

public class LogisticsActivity extends BaseMvpActivity<OrderView, OrderPresenter> implements OrderView, View.OnClickListener {


    private ListView listView;
    private QuickAdapter<LogisticsBean.Info> adapter;
    private TextView txtNoData,txtExpressNumber;


    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public OrderPresenter initPresenter() {
        return new OrderPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_logistics);
        TextView title = (TextView) findViewById(R.id.title);
        title.setText("查看物流");
        findViewById(R.id.fl_Left).setOnClickListener(this);

        listView = (ListView) findViewById(R.id.listView);
        listView.setDivider(getResources().getDrawable(R.color.transparent));
        listView.setSelector(R.color.transparent);
        listView.setDividerHeight(0);
        txtNoData = (TextView) findViewById(R.id.txtNoData);
        txtExpressNumber = (TextView) findViewById(R.id.txtExpressNumber);
        adapter = new QuickAdapter<LogisticsBean.Info>($this, R.layout.item_logistics) {
            @Override
            protected void convert(BaseAdapterHelper helper, LogisticsBean.Info item) {
                helper.setText(R.id.txtTime, item.getTime());
                String address = item.getContext();

                if (helper.getPosition() == 0) {
                    // 第一行头的竖线不显示
                    helper.getView(R.id.tvTopLine).setVisibility(View.INVISIBLE);
                    // 字体颜色加深
                    helper.setTextColor(R.id.txtTime, 0xff555555);
                    helper.setTextColor(R.id.txtContext, 0xff555555);
                    helper.setBackgroundRes(R.id.tvDot, R.drawable.x_timelline_dot_first);
                    helper.setVisible(R.id.tvBottomLine, true);
                } else if (helper.getPosition() == getCount() - 1) {
                    helper.setVisible(R.id.tvTopLine, true);
                    helper.setVisible(R.id.tvBottomLine, false);
                    helper.setTextColor(R.id.txtTime, 0xff999999);
                    helper.setTextColor(R.id.txtContext, 0xff999999);
                    helper.setBackgroundRes(R.id.tvDot, R.drawable.x_timelline_dot_normal);
                } else {
                    helper.setVisible(R.id.tvTopLine, true);
                    helper.setVisible(R.id.tvBottomLine, true);
                    helper.setTextColor(R.id.txtTime, 0xff999999);
                    helper.setTextColor(R.id.txtContext, 0xff999999);
                    helper.setBackgroundRes(R.id.tvDot, R.drawable.x_timelline_dot_normal);
                }

                if (address != null) {
                    String[] arr = address.split("\\|");
                    if (arr.length >= 2) {
                        helper.setText(R.id.txtContext, arr[1]);
                    } else {
                        helper.setText(R.id.txtContext, address);
                    }

                } else {
                    helper.setText(R.id.txtContext, "");
                }

            }
        };

        listView.setAdapter(adapter);

        String expressNumber = getIntent().getStringExtra("expressNumber");
        String expressCode = getIntent().getStringExtra("expressCode");
        TextView txtAddress = (TextView) findViewById(R.id.txtAddress);
        txtAddress.setText(getIntent().getStringExtra("address"));
        txtExpressNumber.setText("物流单号：" + expressNumber);
        presenter.findOrderExpressInfo($this, expressCode, expressNumber, new BaseView.OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                if (content != null && content instanceof LogisticsBean.Data) {
                    LogisticsBean.Data logicData = (LogisticsBean.Data) content;
                    if (logicData.getData() != null && logicData.getData().size() > 0) {
                        adapter.clear();
                        adapter.addAll(logicData.getData());
                        listView.setVisibility(View.VISIBLE);
                        txtNoData.setVisibility(View.GONE);
                    } else {
                        listView.setVisibility(View.GONE);
                        txtNoData.setVisibility(View.VISIBLE);
                    }
                } else {
                    listView.setVisibility(View.GONE);
                    txtNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fl_Left:
                finish();
                break;
        }
    }
}
